package com.himiotech.core.project.web.base;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.himiotech.core.framework.helper.exception.HimioException;
import com.himiotech.core.project.web.base.model.HimioResult;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * @author wuwenbin
 */
class JsonBaseController extends SessionBaseController {

    /**
     * 返回LayuiTable的数据
     *
     * @param page
     * @param <T>
     * @return
     */
    protected <T> HimioResult writeJsonLayuiTable(Page<T> page) {
        if (page.getTotal() <= 0) {
            return HimioResult.errMsgData("无数据", Collections.emptyList());
        } else {
            HimioResult himioResult = HimioResult.okMsgData("获取数据成功", page.getRecords());
            himioResult.put("count", page.getTotal());
            return himioResult;
        }
    }

    /**
     * 返回LayuiTable的数据
     *
     * @param page
     * @param <T>
     * @return
     */
    protected <T> HimioResult writeJsonLayuiTable(Page<T> page, List<T> records) {
        page.setRecords(records);
        page.setTotal(records.size());
        return writeJsonLayuiTable(page);
    }

    /**
     * 返回LayuiTable的数据，不分页
     *
     * @param records
     * @param <T>
     * @return
     */
    protected <T> HimioResult writeJsonLayuiTable(Collection<T> records) {
        Page<T> page = new Page<>(1, 1609);
        page.setRecords(new ArrayList<>(records));
        page.setTotal(records.size());
        return writeJsonLayuiTable(page);
    }

    /**
     * 处理 JSR-303 的 BindingResult 处理的结果错误信息
     *
     * @param result
     * @return
     */
    protected HimioResult writeJsonJsr303(BindingResult result) {
        List<FieldError> fieldErrors = result.getFieldErrors();
        StringBuilder message = new StringBuilder();
        for (FieldError error : fieldErrors) {
            message.append(error.getField()).append(":").append(error.getDefaultMessage()).append("<br/>");
        }
        return HimioResult.errMsg(message.toString());
    }

    protected HimioResult writeJsonEx(Throwable t) {
        return HimioResult.errException(new HimioException(t));
    }

    protected HimioResult writeJsonOk() {
        return HimioResult.ok();
    }

    protected HimioResult writeJsonOk(String message) {
        return HimioResult.okMsg(message);
    }

    protected HimioResult writeJsonOk(String message, Object data) {
        return HimioResult.okMsgData(message, data);
    }

    protected HimioResult writeJsonOkData(Object data) {
        return writeJsonOk("操作成功", data);
    }

    protected HimioResult writeJsonErr(String message, Object... param) {
        return HimioResult.errMsgFormat(message, param);
    }

    protected HimioResult writeJsonErr() {
        return writeJsonErr("操作失败");
    }

    protected HimioResult writeJson(boolean success, String msg, Object data) {
        return HimioResult.create(success ? 0 : -1, msg, data);
    }

    protected HimioResult writeJson(int affectRowCount) {
        return affectRowCount > 0 ? writeJsonOk() : writeJsonErr();
    }

    protected HimioResult writeJson(boolean affectResult) {
        return affectResult ? writeJsonOk() : writeJsonErr();
    }
}
